from django.db import models
from datetime import datetime

# Create your models here.

class Users(models.Model):
    """
    用户表
    """
    id = models.BigAutoField(primary_key=True)
    username = models.CharField(verbose_name="用户名", max_length=32, blank=False, null=False)
    nickname = models.CharField(verbose_name="昵称", max_length=32, null=True, blank=True, default='')
    password = models.CharField(verbose_name="密码", max_length=64, blank=False, null=False)
    avatar = models.CharField(verbose_name="头像", max_length=100, null=True, blank=True, default="/images/avatar/user.png")
    salt = models.CharField(verbose_name="密码盐", max_length=24, blank=False, null=False)
    power = models.IntegerField(verbose_name="剩余算力", null=True, blank=True, default=100)
    expired_time = models.DateTimeField(verbose_name="用户过期时间", null=True, blank=True, default=None)
    status = models.BooleanField(verbose_name="当前状态", default=True)
    chat_config_json = models.TextField(verbose_name="聊天配置json", null=True, blank=True, default='')
    chat_roles_json = models.TextField(verbose_name="聊天角色json", null=True, blank=True, default='')
    chat_models_json = models.TextField(verbose_name="AI模型json", null=True, blank=True, default='')
    last_login_at = models.DateTimeField(verbose_name="最后登录时间", null=True, blank=True, default=None)
    last_login_ip = models.GenericIPAddressField(verbose_name="最后登陆ip", null=True, blank=True, default='')
    vip = models.BooleanField(verbose_name="是否会员", default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.username


# 用户组
class UsersGroup(models.Model):
    """
    用户组表
    定义：
    1，控制组内的用户 免费使用的模型
    2，控制组内的用户 免费使用模型的有效期
    """
    id = models.BigAutoField(primary_key=True)
    groupname = models.CharField(verbose_name="组名", max_length=32, blank=False, null=False)
    user_expired_time = models.DateTimeField(verbose_name="关联用户过期时间", null=True, blank=True, default=None)
    chat_roles_json = models.TextField(verbose_name="聊天角色json", null=True, blank=True, default='')
    chat_models_json = models.TextField(verbose_name="AI模型json", null=True, blank=True, default='')
    status = models.BooleanField(verbose_name="是否启用", default=False)

    users = models.ManyToManyField(Users, related_name='userList')

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

